package com.xyzyzglxt.web.controller.volunteer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.xyzyzglxt.common.core.domain.entity.SysUser;
import com.xyzyzglxt.common.utils.DateUtils;
import com.xyzyzglxt.system.domain.SysEvent;
import com.xyzyzglxt.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xyzyzglxt.common.annotation.Log;
import com.xyzyzglxt.common.core.controller.BaseController;
import com.xyzyzglxt.common.core.domain.AjaxResult;
import com.xyzyzglxt.common.enums.BusinessType;
import com.xyzyzglxt.system.domain.SysUserEvent;
import com.xyzyzglxt.system.service.ISysUserEventService;
import com.xyzyzglxt.common.utils.poi.ExcelUtil;
import com.xyzyzglxt.common.core.page.TableDataInfo;

/**
 * 报名情况Controller
 *
 * @author xyzyzglxt
 * @date 2022-01-17
 */
@Api(tags = "报名情况接口")
@RestController
@RequestMapping("/volunteer/user")
public class SysUserEventController extends BaseController {
    @Autowired
    private ISysUserEventService sysUserEventService;

    /**
     * 查询报名情况列表
     */
    @ApiOperation("查询报名情况列表")
    @PreAuthorize("@ss.hasPermi('volunteer:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserEvent sysUserEvent) {
        startPage();
        List<SysUserEvent> list = sysUserEventService.selectSysUserEventList(sysUserEvent);
        return getDataTable(list);
    }


    /**
     * 按条件查询报名情况列表
     */
    @ApiOperation("查询报名情况列表")
    @PreAuthorize("@ss.hasPermi('volunteer:user:list')")
    @PostMapping("/list")
    public TableDataInfo postList(@RequestBody SysUserEvent sysUserEvent) {
        startPage();
        List<SysUserEvent> list = sysUserEventService.selectSysUserEventList(sysUserEvent);
        return getDataTable(list);
    }


    /**
     * 导出报名情况列表
     */
    @ApiOperation("导出报名情况列表")
    @PreAuthorize("@ss.hasPermi('volunteer:user:export')")
    @Log(title = "报名情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserEvent sysUserEvent) {
        List<SysUserEvent> list = sysUserEventService.selectSysUserEventList(sysUserEvent);
        ExcelUtil<SysUserEvent> util = new ExcelUtil<SysUserEvent>(SysUserEvent.class);
        util.exportExcel(response, list, "报名情况数据");
    }

    /**
     * 获取报名情况详细信息
     */
    @ApiOperation("根据userID获取报名情况详细信息")
    @PreAuthorize("@ss.hasPermi('volunteer:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(sysUserEventService.selectSysUserEventByUserId(userId));
    }

    /**
     * 获取报名情况信息
     */
    @ApiOperation("根据自愿者姓名获取报名情况信息")
    @PreAuthorize("@ss.hasPermi('volunteer:user:query')")
    @GetMapping(value = "findByName/{name}/{eventId}")
    public TableDataInfo findByName(@PathVariable("name") String name,@PathVariable("eventId") Long eventId) {
        startPage();
        List<SysUserEvent> sysUserEvents = sysUserEventService.seletSysUserEventByName(name, eventId);
        return getDataTable(sysUserEvents);
    }

    /**
     * 活动报名
     */
    @ApiOperation("活动报名")
    @PreAuthorize("@ss.hasPermi('volunteer:user:report')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PutMapping("/report/{eventId}/{userId}")
    public AjaxResult report(@PathVariable("eventId") Long eventId, @PathVariable("userId") Long userId) {

        int num = sysUserEventService.reportSysUserEvent(eventId, userId);
        if (num == -1) {
            return error("活动未审核");
        }else if (num == 0){
            return error("报名人数已满");
        }else if(num == -2){
            return error("活动已开始或已结束");
        }else if(num == -3){
            return error("你已报名");
        }
        return success();
    }

    /**
     * 新增报名信息
     */
    @ApiOperation("新增报名信息(用于管理员或招募单位新增)")
    @PreAuthorize("@ss.hasPermi('volunteer:user:add')")
    @Log(title = "新增报名信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysUserEvent sysUserEvent) {
        return toAjax(sysUserEventService.insertSysUserEvent(sysUserEvent));
    }

    /**
     * 修改活动信息
     *
     * @param sysUserEvent
     * @return
     */
    @ApiOperation("修改报名信息（给管理员或招募单位的接口）")
    @PreAuthorize("@ss.hasPermi('volunteer:event:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserEvent sysUserEvent) {
        return toAjax(sysUserEventService.updateSysUserEvent(sysUserEvent));
    }

    /**
     * 签到
     */
    @ApiOperation("签到接口(提供自愿者)")
    @PreAuthorize("@ss.hasPermi('volunteer:user:start')")
    @Log(title = "签到", businessType = BusinessType.UPDATE)
    @PutMapping("/sign_up")
    public AjaxResult signUp(@RequestBody SysUserEvent sysUserEvent) {
        int num = sysUserEventService.SignUp(sysUserEvent);

        if (num == -1){
            return error("审核未通过");
        }else if (num == 0){
            return error("活动未开始");
        }else if (num == -2){
            return error("活动结束前十分钟分钟不能签到");
        }else if(num == -3){
            return error("未报名");
        }else if(num == -11){
            return error("只能撤销自己的");
        }else if (num == -12){
            return error("只能操作自己的活动信息");
        }
        return success();
    }

    /**
     * 签退
     */
    @ApiOperation("签退接口（提供自愿者）")
    @PreAuthorize("@ss.hasPermi('volunteer:user:end')")
    @Log(title = "签退", businessType = BusinessType.UPDATE)
    @PutMapping("/sign_out")
    public AjaxResult signOut(@RequestBody SysUserEvent sysUserEvent) {
        int num = sysUserEventService.SignOut(sysUserEvent);
        if (num == -1){
            return error("请先报名");
        }else if (num == -2){
            return error("审核不通过");
        }else if (num == -3){
            return error("未签到");
        }else if (num == 0){
            return error("未到签退时间(活动结束十分钟前后)或签退时间已过期");
        }else if (num == -4){
            return error("修改总时长服务失败");
        }else if(num == -11){
            return error("只能撤销自己的");
        }else if (num == -12){
            return error("只能操作自己的活动信息");
        }
        return success();
    }

    /**
     * 志愿者审核
     */
    @ApiOperation("志愿者审核成功")
    @PreAuthorize("@ss.hasPermi('volunteer:user:check')")
    @Log(title = "志愿者审核", businessType = BusinessType.UPDATE)
    @PostMapping("/check_ok/{userId}/{eventId}")
    public AjaxResult check(@PathVariable Long userId, @PathVariable Long eventId) {
        int num = sysUserEventService.check(userId, eventId);
        if(num == -11){
            return error("只能撤销自己的");
        }else if (num == -12){
            return error("只能操作自己的活动信息");
        }else if(num == -13){
            return error("活动未结束不允许审核");
        }else if(num == -14){
            return error("活动已审核");
        }else if(num == -15){
            return error("未签退");
        }else if(num == -16){
            return error("请上传照片");
        }
        return success();
    }

    /**
     * 志愿者审核
     */
    @ApiOperation("志愿者审核失败")
    @PreAuthorize("@ss.hasPermi('volunteer:user:check')")
    @Log(title = "志愿者审核", businessType = BusinessType.UPDATE)
    @PostMapping("/check_fail/{userId}/{eventId}")
    public AjaxResult check_fail(@PathVariable Long userId, @PathVariable Long eventId) {
        int num = sysUserEventService.check_fail(userId, eventId);
        if(num == -11){
            return error("只能撤销自己的");
        }else if (num == -12){
            return error("只能操作自己的活动信息");
        }else if(num == -13){
            return error("活动未结束不允许审核");
        }else if(num == -14){
            return error("活动已审核");
        }else if(num == -15){
            return error("未签退");
        }else if(num == -16){
            return error("请上传照片");
        }
        return success();
    }


    /**
     * 撤销报名
     */
    @ApiOperation("撤销报名")
    @PreAuthorize("@ss.hasPermi('volunteer:user:remove')")
    @Log(title = "撤销报名", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{userId}/{eventId}")
    public AjaxResult add(@PathVariable("userId") Long userId, @PathVariable("eventId") Long eventId) {
        int num = sysUserEventService.removeUserEvent(userId, eventId);
        if (num == -11){
            return error("只能撤销自己的");
        }else if (num == -12){
            return error("招募单位只能撤销自己的活动报名信息");
        }else if(num == -5){
            return error("活动进行时不允许撤销报名");
        }else if(num == -4){
            return error("审核通过不允许撤销报名");
        }
        return success();
    }




}
